

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.DoorPoint;
import com.loong.loong.hik.service.DoorPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 门禁点
 *
 * @author jinyiang
 * @date 2023-03-29 17:23:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/doorpoint" )
@Api(value = "doorpoint", tags = "门禁点管理")
public class DoorPointController {

    private final  DoorPointService doorPointService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param doorPoint 门禁点
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_doorpoint_view')" )
    public R getDoorPointPage(Page page, DoorPoint doorPoint) {
		return R.ok(doorPointService.page(page,
				Wrappers.query(doorPoint).lambda()
						.like(StringUtils.isNotEmpty(doorPoint.getDoorName()),DoorPoint::getDoorName,doorPoint.getDoorName())
						.between(StringUtils.isNotEmpty(doorPoint.getStartTime())&&StringUtils.isNotEmpty(doorPoint.getEndTime()),DoorPoint::getCreateTime,doorPoint.getStartTime(),doorPoint.getEndTime())));
    }


    /**
     * 通过id查询门禁点
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_doorpoint_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(doorPointService.getById(id));
    }

    /**
     * 新增门禁点
     * @param doorPoint 门禁点
     * @return R
     */
    @ApiOperation(value = "新增门禁点", notes = "新增门禁点")
    @SysLog("新增门禁点" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_doorpoint_add')" )
    public R save(@RequestBody DoorPoint doorPoint) {
        return R.ok(doorPointService.save(doorPoint));
    }

    /**
     * 修改门禁点
     * @param doorPoint 门禁点
     * @return R
     */
    @ApiOperation(value = "修改门禁点", notes = "修改门禁点")
    @SysLog("修改门禁点" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_doorpoint_edit')" )
    public R updateById(@RequestBody DoorPoint doorPoint) {
        return R.ok(doorPointService.updateById(doorPoint));
    }

    /**
     * 通过id删除门禁点
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除门禁点", notes = "通过id删除门禁点")
    @SysLog("通过id删除门禁点" )
	@DeleteMapping
    @PreAuthorize("@pms.hasPermission('hik_doorpoint_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(doorPointService.removeById(id));
    }
    /**
     * 控制门禁状态
     * @param doorCode
	 * @param num 2 开启， 1 关闭，0 常开，3常闭
     * @return R
     */
    @ApiOperation(value = "控制门禁", notes = "控制门禁")
    @SysLog("控制门禁")
    @GetMapping("/controller" )
    public R controller(@RequestParam String doorCode,@RequestParam Integer num) {
        return R.ok(doorPointService.controller(doorCode,num));
    }


    /**
     * 导出excel 表格
     * @param doorPoint 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_doorpoint_export')" )
    public List<DoorPoint> export(DoorPoint doorPoint) {
        return doorPointService.list(Wrappers.query(doorPoint));
    }
}
